package com.atguigu.yygh.cmn.controller.admin;


import com.atguigu.yygh.cmn.service.DictService;
import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.model.cmn.Dict;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 组织架构表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2023-03-17
 */
@RestController
@RequestMapping("/admin/cmn")
public class DictController {

    @Autowired
    private DictService dictService;

    @GetMapping("/children/{pid}")
    public R getChildrenByPid(@PathVariable Long pid){
        long start = System.currentTimeMillis();
        List<Dict> dicts= dictService.getChildrenByPid(pid);
        long end = System.currentTimeMillis();
        System.out.println("总共花费:"+(end-start)+"ms");
       return R.ok().data("items",dicts);
    }

    @GetMapping("/download")
    public void  download(HttpServletResponse response) throws Exception {
        dictService.download(response);
    }

    @PostMapping("/upload")
    public R upload(MultipartFile file) throws IOException {
        dictService.upload(file);
        return R.ok();
    }

    //将省区市编号查询对应的文字
    @GetMapping("/name/{value}")
    public String getNameByValue(@PathVariable("value") Long value){
      return dictService.getNameByValue(value);
    }

    //根据医院等级编号查询对应的文字
    @GetMapping("/name/{dictCode}/{value}")
    public String getNameByDictCodeAndValue(@PathVariable("dictCode") String dictCode, @PathVariable("value") Long value){
        return dictService.getNameByDictCodeAndValue(dictCode,value);
    }

    //查詢全部的省份信息
    @GetMapping("/{dictCode}")
    public R getAllProvince(@PathVariable String dictCode){
        List<Dict> dictList = dictService.getAllProvince(dictCode);
        return R.ok().data("items",dictList);
    }
}

